<template>
  <!--委外催收案件管理-->
  <section class="page system-parameter-manage">
    <data-form :model="queryModel" @handleResetForm="follFeedback" @onSearch="refreshData" autoSearch>
      <!--输入区域-->
      <template slot="inputs">
        <el-form-item label="批次号" prop="outBatch">
          <el-input v-model="queryModel.outBatch"></el-input>
        </el-form-item>
        <el-form-item label="订单编号" prop="caseNumber">
          <el-input v-model="queryModel.caseNumber"></el-input>
        </el-form-item>
        <el-form-item label="客户姓名" prop="personalName">
          <el-input v-model="queryModel.personalName"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="mobileNo">
          <el-input v-model="queryModel.mobileNo"></el-input>
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="queryModel.idCard"></el-input>
        </el-form-item>
        <el-form-item label="地区" prop="area">
          <el-input v-model="queryModel.area"></el-input>
        </el-form-item>
        <el-form-item label="催收员" prop="collectorRealName">
          <el-input v-model="queryModel.collectorRealName"></el-input>
        </el-form-item>
        <el-form-item label="受托方" prop="outsourceId">
          <el-select clearable placeholder="请选择" v-model="queryModel.outsourceId">
            <el-option :key="item.id" :label="item.outsName" :value="item.id" v-for="item in outSourceList"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="案件金额" prop="caseMoney">
          <number-range :isInteger="true" v-model="queryModel.caseMoney"></number-range>
        </el-form-item>
        <el-form-item label="欠款金额" prop="overdueAmount">
          <number-range :isInteger="true" v-model="queryModel.overdueAmount"></number-range>
        </el-form-item>
        <el-form-item label="逾期期数" prop="overduePeriod">
          <el-select clearable placeholder="请选择" v-model="queryModel.overduePeriod">
            <el-option :key="index" :label="name" :value="code.replace(/M|\+/, '')" v-for="{index,name,code} in $dict.getDictData('OverduePeriodStatus')"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="逾期天数" prop="overdueDays">
          <number-range v-model="queryModel.overdueDays"></number-range>
        </el-form-item>
        <el-form-item label="行动代码" prop="followupBack">
          <el-select clearable placeholder="所有" v-model="queryModel.followupBack">
            <el-option :key="index" :label="name" :value="code" v-for="{index,name,code} in $dict.getDictData('FollowupBack')"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="产品类型" prop="productCategory">
          <el-input v-model="queryModel.productCategory"></el-input>
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="queryModel.productName"></el-input>
        </el-form-item>
        <el-form-item label="案件流入时间" prop="followInTime">
          <el-date-picker :editable="false" clearable placeholder="选择日期时间" type="daterange" v-model="queryModel.followInTime"></el-date-picker>
        </el-form-item>
        <el-form-item label="委外时间" prop="outTimeDate">
          <el-date-picker :editable="false" clearable placeholder="选择日期时间" type="daterange" v-model="queryModel.outTimeDate"></el-date-picker>
        </el-form-item>
        <el-form-item label="委外到期时间" prop="outEndDate">
          <el-date-picker :editable="false" clearable placeholder="选择日期时间" type="daterange" v-model="queryModel.outEndDate"></el-date-picker>
        </el-form-item>
        <el-form-item label="跟进时间" prop="followUpTime">
          <el-date-picker :editable="false" clearable placeholder="选择日期时间" type="daterange" v-model="queryModel.followUpTime"></el-date-picker>
        </el-form-item>
        <el-form-item label="留案标志" prop="leaveCaseFlag">
          <el-select clearable placeholder="所有" v-model="queryModel.leaveCaseFlag">
            <el-option :key="index" :label="name" :value="code" v-for="{index,name,code} in $dict.getDictData('CommonFlag')"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="案件标签" prop="labelName">
          <get-label-name :formModel="queryModel" v-model="queryModel.labelName"></get-label-name>
        </el-form-item>
      </template>
    </data-form>
    <data-box :data="collectionDataList" :page="pageService" :selectionList.sync="selectionList" @onPageChange="refreshData">
      <template slot="buttons">
        <el-button @click="caseRecoveryClick" v-auth="1013">回收案件</el-button>
        <el-button @click="caseStayClick" v-auth="1014">留案</el-button>
        <el-button @click="unCaseStayClick" v-auth="1015">取消留案</el-button>
        <el-button @click="importMobileUrgeClick" v-auth="1016">导入电催跟进</el-button>
        <el-button @click="importOutVisitClick" v-auth="1017">导入外访跟进</el-button>
        <el-button @click="batchRepaymentClick" v-auth="228">批量还款</el-button>
      </template>
      <template slot="columns">
        <!--数据列区域-->
        <el-table-column :min-width="$helper.getColumnWidth(6)" label="订单编号">
          <template slot-scope="scope">
            <el-button @click="openExecutionPage(scope.row)" type="text">{{scope.row.caseNumber}}</el-button>
          </template>
        </el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(4)" label="批次号" prop="outBatch"></el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(3)" label="受托方" prop="outsName" show-overflow-tooltip></el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(3)" label="客户姓名" prop="personalName"></el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(4)" label="手机号" prop="mobileNo"></el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(5)" label="身份证号" prop="idCard"></el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(4)" label="地区" prop="area" show-overflow-tooltip></el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(4)" label="案件金额(元)" prop="caseMoney"></el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(4)" label="欠款金额(元)" prop="overdueAmount"></el-table-column>
        <el-table-column :formatter="row => $common.isNotValue(row.overduePeriod) ? '':`M${row.overduePeriod}`" :min-width="$helper.getColumnWidth(3)" label="逾期期数" prop="overduePeriod"></el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(3)" label="逾期天数" prop="overdueDays"></el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(3)" label="产品类型" prop="productCategory"></el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(3)" label="产品名称" prop="productName"></el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(5)" label="案件流入时间" prop="followInTime">
          <template slot-scope="scope">
            <span>{{scope.row.followInTime | dateTimeFormat}}</span>
          </template>
        </el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(5)" label="委外分配时间" prop="caseAllocationTime">
          <template slot-scope="scope">
            <span>{{scope.row.caseAllocationTime | dateTimeFormat}}</span>
          </template>
        </el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(5)" label="委外到期时间" prop="outEndDate">
          <template slot-scope="scope">
            <span>{{scope.row.outEndDate | dateFormat}}</span>
          </template>
        </el-table-column>
        <el-table-column :formatter="(row) => $filter.dateTimeFormat(row.followUpTime)" :min-width="$helper.getColumnWidth(5)" label="跟进时间" prop="followUpTime"></el-table-column>
        <el-table-column :formatter="(row) => $filter.dictConvert(row.followupBack,'FollowupBack')" :min-width="$helper.getColumnWidth(3)" label="行动代码" prop="followupBack"></el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(3)" label="留案标识" prop="leaveCaseFlag">
          <template slot-scope="scope">{{scope.row.leaveCaseFlag =='YES'?'是':'否'}}</template>
        </el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(5)" label="留案到期日期" prop="leaveDueDate">
          <template slot-scope="scope">
            <span>{{scope.row.leaveDueDate | dateFormat}}</span>
          </template>
        </el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(10)" label="案件标签" prop="labelName"></el-table-column>
        <el-table-column :min-width="$helper.getColumnWidth(5)" label="操作">
          <template slot-scope="scope">
            <el-button @click="repaymentClick(scope.row)" type="text" v-auth="229">还款</el-button>
          </template>
        </el-table-column>
      </template>
    </data-box>

    <!--弹框-留案-->
    <el-dialog :close-on-click-modal="false" :show-close="false" :visible.sync="dialog.leaveCase" @close="dialog.leaveCase = false" title="留案">
      <leave-case :selectionList.sync="selectionList" @close="dialog.leaveCase=false" @refreshList="refreshData"></leave-case>
    </el-dialog>
    <el-dialog :close-on-click-modal="false" :show-close="false" :visible.sync="dialog.uploadFile" @close="dialog.uploadFile = false" title="上传文件">
      <file-upload :AllowExtension="['xlsx','xls']" :fileList.sync="fileList" :fileNumberLimit="1" :showSM="showSMData" ref="upload_case_import"></file-upload>
      <div class="btn-group">
        <el-button @click="commit">确定</el-button>
        <el-button @click="close">取消</el-button>
      </div>
    </el-dialog>
    <!-- 批量还款弹窗 -->
    <el-dialog :center="true" :close-on-click-modal="false" :close-on-press-escape="false" :show-close="false" :visible.sync="dialog.batchRepayment" @open="$nextTick(()=>$refs['team-case-batch-repayment'].updateList(selectCaseList))" title="批量还款" width="800px">
      <team-case-batch-repayment @close="dialogClose" ref="team-case-batch-repayment"></team-case-batch-repayment>
    </el-dialog>
    <!-- 还款弹窗 -->
    <el-dialog :center="true" :close-on-click-modal="false" :close-on-press-escape="false" :show-close="false" :visible.sync="dialog.repayment" @open="$nextTick(()=>$refs['team-case-repayment'].update(teamCaseObj))" title="还款" width="400px">
      <team-case-repayment @close="refreshData();dialog.repayment=false;teamCaseObj={}" ref="team-case-repayment"></team-case-repayment>
    </el-dialog>
  </section>
</template>

<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
import { Dependencies, Layout, Auth } from "~/core/decorator";

import NumberRange from "~/components/common/number-range.vue";
import leaveCase from "~/components/outsource-manage/collection-manage/leave-case.vue"; // 留案
import { namespace, State } from "vuex-class";
import { PageService } from "~/utils/page.service";
import { SortService } from "~/utils/sort.service";
import { OutsourceCollectionService } from "~/services/domain-service/outsource-collection-manage.service";
import FileUpload from "~/components/common/file-upload.vue";
import { CityService } from "~/utils/city.service";
import { OutsourceSettleManageService } from "~/services/domain-service/outsource-settle-manage.service";
import { CASE_CHANNEL, MENU_CHANNEL } from "~/config/enum.config";
import GetLabelName from "~/components/labeling-strategy/get-label-name.vue";
import TeamCaseBatchRepayment from "~/components/visit-case/team-case-batch-repayment.vue";
import TeamCaseRepayment from "~/components/visit-case/team-case-repayment.vue";

@Auth(1003)
@Layout("workspace")
@Component({
  components: {
    GetLabelName,
    NumberRange,
    leaveCase,
    FileUpload,
    TeamCaseBatchRepayment,
    TeamCaseRepayment
  }
})
export default class CollectionManageIndex extends Vue {
  @Dependencies(OutsourceSettleManageService)
  private OutsourceSettleManageService: OutsourceSettleManageService;
  @Dependencies(OutsourceCollectionService)
  private outsourceCollectionService: OutsourceCollectionService;
  @Dependencies(PageService) private pageService: PageService;
  @Dependencies(SortService) private sortService: SortService;
  @State private outSourceList;

  private dialog: any = {
    leaveCase: false,
    uploadFile: false,
    batchRepayment: false,
    repayment: false
  };
  private fileList: any = [];
  private showSMData: any = "";
  private selectCaseList: any = [];
  private teamCaseObj: any = {};

  private proinviceList: any[] = []; // 省
  private cityList: any[] = []; //市

  private queryModel: any = {
    outBatch: "",
    caseNumber: "",
    personalName: "",
    area: "",
    mobileNo: "",
    idCard: "",
    localAddress: "",
    currentCollector: "",
    outsourceId: "",
    overduePeriod: "",
    turnSource: "",
    productCategory: "",
    productName: "",
    proinviceCode: "",
    cityCode: "",
    followupBack: "",
    overdueAmount: {
      max: "",
      min: ""
    },
    caseMoney: { min: "", max: "" },
    overdueDays: {
      max: "",
      min: ""
    },
    collectorRealName: "",
    customerCategory: "",
    leaveCaseFlag: "",
    followInTime: [],
    outEndDate: [],
    outTimeDate: [],
    followUpTime: [],
    labelName: []
  };

  private collectionDataList: any[] = []; //数据列表
  private selectionList: any[] = []; //选中数据
  private caseIds: any[] = []; //选择的案件号数组
  private importFlag: any = ""; //选择导入标识

  private mounted() {
    this.refreshData();
    this.getProvinces();
  }

  //获取省数据
  private getProvinces() {
    this.proinviceList = CityService.getCityData({ level: 1 });
  }

  //获取市
  private getCitys(provinceId) {
    if (this.queryModel.cityCode) {
      this.queryModel.cityCode = "";
    }
    this.cityList = this.$city.getCityData({ level: 1, id: provinceId }); // 城市列表
  }

  private refreshData() {
    // 默认加载列表
    this.outsourceCollectionService
      .earchCaseOutList(this.queryModel, this.pageService, this.sortService)
      .subscribe(data => {
        this.collectionDataList = data;
        this.selectionList = [];
      });
  }

  // 关闭弹框
  private close() {
    const upload: any = this.$refs["upload_case_import"];
    upload.reset();
    this.dialog.uploadFile = false;
  }
  /**
   * 批量还款
   */
  private batchRepaymentClick() {
    if (this.selectionList.length !== 0) {
      this.dialog.batchRepayment = true;
      this.selectCaseList = this.selectionList;
    } else {
      this.$message(`请选择要批量还款的案件`);
    }
  }
  /**
   * 批量还款弹窗关闭回调
   */
  private dialogClose() {
    this.refreshData();
    this.dialog.batchRepayment = false;
    this.selectionList = [];
  }
  /**
   * 还款
   */
  private repaymentClick(row) {
    this.dialog.repayment = true;
    this.teamCaseObj = row;
  }

  // 导入电催跟进
  private commit() {
    if (this.fileList.length === 0) {
      this.$message("请上传导入文件");
      return;
    }
    const query = {
      fileId: this.fileList[0].response.id
    };
    if (this.importFlag === "importMobileUrge") {
      // 导入电催跟进
      this.outsourceCollectionService
        .importMobileUrge(query)
        .subscribe(data => {
          //          this.dialog.uploadFile = false;
          this.close();
          this.refreshData();
          if (data.length !== 0) {
            this.$message.info("存在导入失败的案件号");
          } else {
            this.$message.success("导入成功");
          }
        });
    } else if (this.importFlag === "importOutVisit") {
      // 导入外访跟进
      this.outsourceCollectionService.importOutVisit(query).subscribe(data => {
        //          this.dialog.uploadFile = false;
        this.close();
        this.refreshData();
        if (data.length !== 0) {
          this.$message.info("存在导入失败的案件号");
        } else {
          this.$message.success("导入成功");
        }
      });
    } else if (this.importFlag === "importOutVisitEnclosure") {
      // 导入外访附件
      this.outsourceCollectionService
        .importOutVisitEnclosure(query)
        .subscribe(data => {
          this.dialog.uploadFile = false;
          this.refreshData();
        });
    }
  }

  //回收案件
  private caseRecoveryClick() {
    if (this.selectionList.length === 0) {
      this.$message.info("请选择要回收的案件");
      return;
    }

    const caseIds: any[] = this.selectionList.map(v => v.id);
    const query = {
      caseIds
    };
    this.outsourceCollectionService.caseRecovery(query).subscribe(data => {
      this.$message.success("回收案件成功!");
      this.refreshData();
    });
  }

  //留案
  private caseStayClick() {
    if (this.selectionList.length === 0) {
      this.$message.info("请选择需要留案的案件");
      return;
    }
    this.dialog.leaveCase = true;
  }

  //取消留案
  private unCaseStayClick() {
    if (this.selectionList.length === 0) {
      this.$message.info("请选择需要取消留案的案件");
      return;
    }
    const caseIds: any[] = this.selectionList.map(v => v.id);
    const query = {
      caseIds
    };
    this.outsourceCollectionService.unCaseStay(query).subscribe(data => {
      this.$message.success("取消留案成功!");
      this.refreshData();
    });
  }

  //导入电催跟进
  private importMobileUrgeClick() {
    this.showSMData = 3;
    this.dialog.uploadFile = true;
    this.importFlag = "importMobileUrge";
  }

  //导入外访跟进
  private importOutVisitClick() {
    this.showSMData = 4;
    this.dialog.uploadFile = true;
    this.importFlag = "importOutVisit";
  }

  //导入外访附件
  private importOutVisitEnclosureClick() {
    this.dialog.uploadFile = true;
    this.importFlag = "importOutVisitEnclosure";
  }

  /**
   * 进入查看执行页
   */
  private openExecutionPage(row) {
    this.$router.openCaseDetail(row.id, {
      menuChannel: MENU_CHANNEL.Outsource,
      hiddenCaseList: true,
      hiddenAction: true
    });
  }

  /**
   * 重置numberRange
   */
  private follFeedback() {
    this.queryModel.overdueAmount = { min: "", max: "" };
    this.queryModel.overdueDays = { min: "", max: "" };
    this.queryModel.proinviceCode = "";
    this.queryModel.cityCode = "";
    this.queryModel.outsourceId = "";
    this.queryModel.caseMoney = { min: "", max: "" };
  }
}
</script>

<style lang="less">
.btn-group {
  display: flex;
  justify-content: flex-end;
  padding-top: 20px;
}
</style>
